package com.jack.view

import com.google.gson.Gson
import com.jack.service.HomeArticleService
import javafx.scene.Parent
import kotlinx.coroutines.*
import kotlinx.coroutines.javafx.JavaFx
import tornadofx.*

class HomeArticleView : View("") {
    private val coreScope = CoroutineScope(SupervisorJob() + Dispatchers.JavaFx.immediate)

    private val service by di<HomeArticleService>()

    override val root: Parent = vbox {
        prefWidth = 500.0
        prefHeight = 300.0

        button("click").setOnAction {
            fetch()
        }

        button("click2").setOnAction {
            println("click2")
        }
    }

    private fun fetch() {
        coreScope.launch(Dispatchers.JavaFx.immediate) {
            println("startLoading....")
            val result = service.fetchData()
            println(Gson().toJson(result))
        }
    }

    override fun onUndock() {
        coreScope.cancel()
        super.onUndock()
    }

}